package pe.edu.upc.dew.grupo7.web;

import java.util.List;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo7.model.Funcion;
import pe.edu.upc.dew.grupo7.model.Pelicula;
import pe.edu.upc.dew.grupo7.service.FuncionService;
import pe.edu.upc.dew.grupo7.service.PeliculaService;

/**
 * Servlet implementation class CarteleraServlet
 */
public class CarteleraServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PeliculaService peliculaService;
	private FuncionService funcionService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CarteleraServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init(ServletConfig config) throws ServletException {

		this.peliculaService = WebApplicationContextUtils
				.getWebApplicationContext(config.getServletContext()).getBean(
						"peliculaService", PeliculaService.class);
		this.funcionService = WebApplicationContextUtils
				.getWebApplicationContext(config.getServletContext()).getBean(
						"funcionService", FuncionService.class);

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Pelicula> peliculas = peliculaService.getPeliculas();
		request.setAttribute("peliculas", peliculas);
		List<Funcion> funciones = funcionService.getFunciones();
		request.setAttribute("funciones", funciones);
		
		request.getRequestDispatcher("cartelera.jsp")
				.forward(request, response);
	}

}
